package com.learn.demo.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * webSocket消息类型枚举
 *
 * @author banjiawei
 * @date 2021/05/11
 */
@AllArgsConstructor
public enum WebSocketMessageTypeEnum {
    /**
     * webSocket消息类型
     * <p>连接事件</p>
     * <p>关闭连接事件</p>
     * <p>传输消息事件</p>
     * <p>发生错误事件</p>
     */
    ON_OPEN("connection_topic", "connection_event", "连接", 1),
    ON_CLOSE("disconnection_topic", "disconnection_event", "关闭连接", 2),
    ON_MESSAGE("message_topic", "message_event", "传输消息", 3),
    ON_ERROR("error_topic", "error_event", "发生错误", 4);

    /**
     * 消息订阅频道
     */
    @Getter
    private final String topName;

    /**
     * 事件名称
     */
    @Getter
    private final String eventName;

    /**
     * 描述
     */
    @Getter
    private final String describe;

    /**
     * 消息类型
     */
    @Getter
    private final Integer type;

    /**
     * 根据事件名获取枚举类
     * @author banjiawei
     * @date 2021/5/11
     * @param eventName
     * @return com.fhgl.tos.user.enums.WebSocketMessageTypeEnum
     */
    public static WebSocketMessageTypeEnum getWebSocketMessageTypeEnum(String eventName){
        WebSocketMessageTypeEnum[] messageTypeEnums = WebSocketMessageTypeEnum.values();
        for (WebSocketMessageTypeEnum messageTypeEnum : messageTypeEnums) {
            if(messageTypeEnum.eventName.equals(eventName)){
                return messageTypeEnum;
            }
        }
        return null;
    }
}
